package com.mycompany.myapp.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.mycompany.myapp.domain.FinancialManage;
import com.mycompany.myapp.repository.FinancialManageRepository;
import com.mycompany.myapp.web.rest.errors.BadRequestAlertException;
import com.mycompany.myapp.web.rest.util.HeaderUtil;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing FinancialManage.
 */
@RestController
@RequestMapping("/api")
public class FinancialManageResource {

    private final Logger log = LoggerFactory.getLogger(FinancialManageResource.class);

    private static final String ENTITY_NAME = "financialManage";

    private final FinancialManageRepository financialManageRepository;

    public FinancialManageResource(FinancialManageRepository financialManageRepository) {
        this.financialManageRepository = financialManageRepository;
    }

    /**
     * POST  /financial-manages : Create a new financialManage.
     *
     * @param financialManage the financialManage to create
     * @return the ResponseEntity with status 201 (Created) and with body the new financialManage, or with status 400 (Bad Request) if the financialManage has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/financial-manages")
    @Timed
    public ResponseEntity<FinancialManage> createFinancialManage(@RequestBody FinancialManage financialManage) throws URISyntaxException {
        log.debug("REST request to save FinancialManage : {}", financialManage);
        if (financialManage.getId() != null) {
            throw new BadRequestAlertException("A new financialManage cannot already have an ID", ENTITY_NAME, "idexists");
        }
        FinancialManage result = financialManageRepository.save(financialManage);
        return ResponseEntity.created(new URI("/api/financial-manages/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /financial-manages : Updates an existing financialManage.
     *
     * @param financialManage the financialManage to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated financialManage,
     * or with status 400 (Bad Request) if the financialManage is not valid,
     * or with status 500 (Internal Server Error) if the financialManage couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/financial-manages")
    @Timed
    public ResponseEntity<FinancialManage> updateFinancialManage(@RequestBody FinancialManage financialManage) throws URISyntaxException {
        log.debug("REST request to update FinancialManage : {}", financialManage);
        if (financialManage.getId() == null) {
            throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
        }
        FinancialManage result = financialManageRepository.save(financialManage);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, financialManage.getId().toString()))
            .body(result);
    }

    /**
     * GET  /financial-manages : get all the financialManages.
     *
     * @return the ResponseEntity with status 200 (OK) and the list of financialManages in body
     */
    @GetMapping("/financial-manages")
    @Timed
    public List<FinancialManage> getAllFinancialManages() {
        log.debug("REST request to get all FinancialManages");
        return financialManageRepository.findAll();
    }

    /**
     * GET  /financial-manages/:id : get the "id" financialManage.
     *
     * @param id the id of the financialManage to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the financialManage, or with status 404 (Not Found)
     */
    @GetMapping("/financial-manages/{id}")
    @Timed
    public ResponseEntity<FinancialManage> getFinancialManage(@PathVariable Long id) {
        log.debug("REST request to get FinancialManage : {}", id);
        Optional<FinancialManage> financialManage = financialManageRepository.findById(id);
        return ResponseUtil.wrapOrNotFound(financialManage);
    }

    /**
     * DELETE  /financial-manages/:id : delete the "id" financialManage.
     *
     * @param id the id of the financialManage to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/financial-manages/{id}")
    @Timed
    public ResponseEntity<Void> deleteFinancialManage(@PathVariable Long id) {
        log.debug("REST request to delete FinancialManage : {}", id);

        financialManageRepository.deleteById(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }
}
